package org.jtb.mensawotd;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class HistoryDialogBuilder extends AlertDialog.Builder {

	private DetailsActivity activity;
	private Prefs prefs;
	private List<HistoryItem> items;
	
	public HistoryDialogBuilder(DetailsActivity activity, List<HistoryItem> items) {
		super(activity);
		this.activity = activity;
		this.items = items;
		this.prefs = new Prefs(activity);

		this.setTitle("History");
		
		LayoutInflater inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ListView listView = (ListView) inflater.inflate(
				R.layout.history_dialog,
				(ViewGroup) activity.findViewById(R.id.history_list));
		listView.setAdapter(new HistoryAdapter(activity, prefs.getHistory()));
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				HistoryItem item = HistoryDialogBuilder.this.items.get(position);
				HistoryDialogBuilder.this.activity.setFetcherUrl(item.getUrl());
				HistoryDialogBuilder.this.activity.load();
				HistoryDialogBuilder.this.activity.dismissDialog(DetailsActivity.HISTORY_DIALOG);				
			}
		});
		setView(listView);
	}
}
